package cn.wolfcode.luowowo.mgrsite.listen;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定义监听器
 * ContextRefreshedEvent:spring容器启动,并且完成一些相关初始化操作:Ioc	 DI  AOP
 */
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{

    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IStrategyStatisVOService statisVOService;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("攻略数据初始化开始-----------------------------------");
        //将攻略数据信息从mysql中查询出来
        List<StrategyDetail> details = detailService.list();
        //将数据设置到redis中,用到BeanUtil
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : details) {
            //如果redis中已经存在直接跳过
            if (statisVOService.isnotExist(detail.getId())){
                continue;
            }
            vo = new StrategyStatisVO();
            BeanUtil.copyProperties(vo,detail);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            statisVOService.setStrategyStatisVO(vo);
        }
        System.out.println("攻略数据初始化结束-----------------------------------");


        System.out.println("热门攻略推荐数据初始化开始-----------------------------------");
        //将攻略数据信息从mysql中查询出来
        for (StrategyDetail detail : details) {
            //如果zset已经存在,某个column需要跳过
            if (statisVOService.isExistOfValue(detail.getId(), RedisKeys.STRATEGY_ZET_HOT.getPrefix())){
                continue;
            }
            int score = detail.getViewnum() + detail.getReplynum();
            //加分操作
            statisVOService.addHotScores(detail.getId(),score);
        }
        System.out.println("热门攻略推荐数据初始化结束-----------------------------------");

        System.out.println("国内攻略推荐数据初始化开始-----------------------------------");
        //将攻略数据信息从mysql中查询出来
        for (StrategyDetail detail : details) {
            //如果zset已经存在,某个column需要跳过
            if (detail.isIsabroad() || statisVOService.isExistOfValue(detail.getId(), RedisKeys.STRATEGY_ZET_CHINA.getPrefix())){
                continue;
            }
            int score = detail.getFavornum() + detail.getThumbsupnum() ;
            //加分操作
            statisVOService.addCommendScores(detail.getId(),score) ;
        }
        System.out.println("国内攻略推荐数据初始化结束-----------------------------------");

        System.out.println("海外攻略推荐数据初始化开始-----------------------------------");
        //将攻略数据信息从mysql中查询出来
        for (StrategyDetail detail : details) {
            //如果zset已经存在,某个column需要跳过
            if ( !detail.isIsabroad() || statisVOService.isExistOfValue(detail.getId(), RedisKeys.STRATEGY_ZET_ABROAD.getPrefix())){
                continue;
            }
            int score = detail.getFavornum() + detail.getThumbsupnum() ;
            //加分操作
            statisVOService.addCommendScores(detail.getId(),score) ;
        }
        System.out.println("海外攻略推荐数据初始化结束-----------------------------------");
    }
}
